package com.bit.demo.springbookdemo.controller;

import com.bit.demo.springbookdemo.constant.Constant;
import com.bit.demo.springbookdemo.enums.ResultCodeEnum;
import com.bit.demo.springbookdemo.model.*;
import com.bit.demo.springbookdemo.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Book;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo, HttpSession session){
        //1. 校验用户是否登录
        UserInfo userInfo=(UserInfo) session.getAttribute(Constant.SESSION_USER_KEY);
        if(userInfo==null || userInfo.getId()<=0){
            log.warn("添加图书失败,用户未登录");
            return "用户未登录";
        }
        log.info("添加图书:{}",bookInfo);
        //2. 参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())){
            log.warn("添加图书失败,书名不能为空");
            return "书名不能为空";
        }
        if(!StringUtils.hasLength(bookInfo.getAuthor())){
            log.warn("添加图书失败,作者不能为空");
            return "作者不能为空";
        }
        if(!StringUtils.hasLength(bookInfo.getPublish())){
            log.warn("添加图书失败,出版社不能为空");
            return "出版社不能为空";
        }
        if(bookInfo.getCount()==null){
            log.warn("添加图书失败,数量不能为空");
            return "数量不能为空";
        }
        if(bookInfo.getPrice()==null){
            log.warn("添加图书失败,价格不能为空");
            return "价格不能为空";
        }
        //3.存储数据
        try {
            bookService.addBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("添加图书失败:",e);
            return "添加失败";
        }

    }

    @RequestMapping("/getListByPage")
    public Result<ResponseResult<BookInfo>> getListByPage(PageRequest pageRequest, HttpSession session){
//        if(session.getAttribute(Constant.SESSION_USER_KEY)==null){
//            log.warn("查询图书失败,用户未登录");
//            return Result.unLogin();
//        }
//        UserInfo userInfo=(UserInfo) session.getAttribute(Constant.SESSION_USER_KEY);
//        if(userInfo==null || userInfo.getId()<=0){
//            log.warn("查询图书失败,用户未登录");
//            return Result.unLogin();
//        }
        //查询数据
        ResponseResult<BookInfo> responseResult = bookService.getBookByPage(pageRequest);
        return Result.success(responseResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据id查询图书:{}",bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书:{}",bookInfo);
        try {
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("更新图书失败:",e);
            return "更新失败";
        }
    }

    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("删除图书:{}",bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(0);
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("删除图书失败:",e);
            return "删除失败";
        }
    }

    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除图书:{}",ids);
        try {
            bookService.batchDelete(ids);
            return true;
        } catch (Exception e) {
            log.error("批量删除图书失败:",e);
            return false;
        }
    }
}
